package com.cmwa.scbp.sys.organization.controller;

import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cmwa.scbp.base.constants.Constant;
import com.cmwa.scbp.base.mvc.controller.BaseController;
import com.cmwa.scbp.base.mvc.entity.Result;
import com.cmwa.scbp.sys.organization.entity.CompanyVo;
import com.cmwa.scbp.sys.organization.entity.OrganizationVo;
import com.cmwa.scbp.sys.organization.service.CompanyService;
import com.cmwa.scbp.sys.organization.service.OrganizationService;
import com.cmwa.scbp.utils.PinYinUtil;
import com.cmwa.scbp.utils.Utils;

import net.sf.json.JSONObject;

@Controller
@RequestMapping(value = "/sys/organization")
public class OrganizationController extends BaseController {
	
	private final static String PAGE_PATH = "system/sys/organization/";
	
	@Resource
	private OrganizationService organizationService;
	@Resource
	private CompanyService companyService;

	@RequestMapping("/queryOrgByEmpId")
	public void queryOrgByEmpId(@RequestParam("empId") String empId, HttpServletResponse response) throws Exception {
		OrganizationVo org = organizationService.queryOrgByEmpId(empId);
		PrintWriter out = response.getWriter();
		response.setContentType("application/json;charset=UTF-8");
		JSONObject jsonObj = new JSONObject();
		jsonObj.put("result", org);
		out.write(jsonObj.toString());
	}
	
	@RequestMapping("/generateOrgIdPinyin")
	@ResponseBody
	public Result<Object> generateOrgIdPinyin(@RequestParam("orgName") String orgName) throws Exception {
		
		String orgNamePinYin = PinYinUtil.getPinYinFS(orgName);
		if(StringUtils.isNotBlank(orgNamePinYin) && orgNamePinYin.length() > 10) {
			orgNamePinYin = orgNamePinYin.substring(0, 10);
		}
		
		OrganizationVo org = organizationService.getByOrgId(orgNamePinYin);
		
		if(org != null) {
			String orgId = org.getOrgId();
			orgNamePinYin = Utils.generateCodePinyinStr(orgId);
		}
		
		
		Result<Object> result = new Result<Object>();
		result.setObject(orgNamePinYin);
		return result;
	}
	
	/**
	 * 外部机构新增页面
	 * @author ex-hejm
	 * @date 2019年2月19日下午4:24:56
	 * @param model
	 * @return
	 */
	@RequestMapping("/outOrgAddView")
	public String outOrgAddView(ModelMap model) {
		
		List<OrganizationVo> orgTree = organizationService.queryOrgTreeList();
		model.put("orgTree", orgTree);
		
		return PAGE_PATH + "outOrgAdd";
	}

	/**
	 * 修改机构拓展属性页面
	 * @author ex-hejm
	 * @date 2019年2月22日上午10:12:49
	 * @param firmId
	 * @param model
	 * @return
	 */
	@RequestMapping("/modifyFirmExtInfoView")
	public String modifyFirmExtInfoView(@RequestParam("firmId") String firmId, ModelMap model) {
		CompanyVo c = companyService.findCompanyById(firmId.substring(Constant.USERTREE_FIRM_PREFIX.length()).split(Constant.USERTREE_SPILT)[0]);
		model.addAttribute("c", c);
		return PAGE_PATH + "modifyFirmExtInfo";
	}
	
	/**
	 * 新增外部机构
	 * @author ex-hejm
	 * @date 2019年2月19日下午6:30:43
	 * @param org
	 * @return
	 */
	@RequestMapping("/insertOutOrg")
	public String insertOutOrg(OrganizationVo org) {
		organizationService.insertOutOrg(org);
		return redirectSuccess();
	}
	
	@RequestMapping("/modifyOutOrgExt")
	public String modifyOutOrgExt(CompanyVo org) {
		companyService.modifyOutOrgExt(org);
		return redirectSuccess();
	}
	
	@RequestMapping("/checkRepeatOrgId")
	@ResponseBody
	public Result<Object> checkRepeatOrgId(@RequestParam(value = "orgId", required = false) String orgId, @RequestParam(value = "orgName", required = false) String orgName) {
		boolean isRepeat = organizationService.checkRepeatOrgId(orgId, orgName);
		Result<Object> result = new Result<Object>();
		result.setSuccess(isRepeat);
		return result;
	}
	@RequestMapping("/checkRepeatTitleId")
	@ResponseBody
	public Result<Object> checkRepeatTitleId(@RequestParam(value = "titleId", required = false) String titleId, @RequestParam(value = "orgId", required = false) String orgId) {
		boolean isRepeat = organizationService.checkRepeatTitleId(orgId, titleId);
		Result<Object> result = new Result<Object>();
		result.setSuccess(isRepeat);
		return result;
	}
	
	@RequestMapping("/checkRepeatOrgName")
	@ResponseBody
	public Result<Object> checkRepeatOrgName(@RequestParam("orgId") String orgId, @RequestParam("orgName") String orgName) {
		boolean isRepeat = organizationService.checkRepeatOrgName(orgId, orgName);
		Result<Object> result = new Result<Object>();
		result.setSuccess(isRepeat);
		return result;
	}
	
	@RequestMapping("/checkRepeatCompanyFullName")
	@ResponseBody
	public Result<Object> checkRepeatCompanyFullName(@RequestParam("orgId") String orgId, @RequestParam("companyFullName") String companyFullName) {
		boolean isRepeat = companyService.checkRepeatCompanyFullName(orgId, companyFullName);
		Result<Object> result = new Result<Object>();
		result.setSuccess(isRepeat);
		return result;
	}

}
